<?php

  // Pour ajouter des équipes
  // auteur : sebastien Mosser
  
class fAddTeam extends aFeature {

   public function __construct() {
      $this->_acl = new AccessControlList();
      $this->_acl->setGranted(7);
      $this->_acl->setGranted(3);
      $this->_acl->setGranted(2);
   }

   public static function getTocEntry() {
      return "Créer une équipe";
   }

   public static function getRealName() {
		return "Ajouter une équipe";
	}
	
	public static function getSemantic() {
		$res  = "Cette fonctionnalité permet d'ajouter une équipe dans le système Balloon";
		return $res;
	}


  protected function run() {
  	$action = PropRetriever::getProp($_POST,"action");
  	if ($action == "add")
  		return $this->addTeam();
 		return $this->displayForm();
	}


	private function addTeam() {
	  $teamName  = PropRetriever::getProp($_POST,"teamName");
		$teamCat   = PropRetriever::getProp($_POST,"teamCat");
		$teamAdmin = PropRetriever::getProp($_POST,"teamAdmin");
		$coachId   = PropRetriever::getProp($_POST,"coachId");
		$d1Id      = PropRetriever::getProp($_POST,"d1Id");
		$d2Id      = PropRetriever::getProp($_POST,"d2Id");
		
		$errors = array();
		
		if ($teamName == "")
			$errors["teamName"] = "Le nom de l'équipe ne peut être vide !";
		if ($teamCat == -1)
			$errors["teamCat"] = 
							"Une équipe doit obligatoirement appartenir a une catégorie !";
		
		if (count($errors) != 0)
			return $this->displayForm($errors);
		
		// On fait l'ajout : 
		$season = Login::seasonChoosen();
		$aTeam = new Team();
		$aTeam->setAdmin($teamAdmin);
		$aTeam->setName(ucwords(strtolower($teamName)));
		$aTeam->setCategory($teamCat);
		$aTeam->setCoach($coachId);
		$aTeam->setDirector1($d1Id);
		$aTeam->setDirector2($d2Id);
		$aTeam->setPicture();
		
		// A t'on déja ajouté cette équipe ??
		$lastInsert = Login::lastInsert();
		if ($lastInsert["ClassName"] == "Team") {
			$exist = True;
			
			try {
				$lastTeam = Team::read($lastInsert["LastId"]);
			}
			catch(Exception $e) {
			  $exist = False;
			}
			
			if ($exist and $lastTeam->soundsLike($aTeam)) {
				$aTeam = $lastTeam;
			}
			else {
				$aTeam->create();
			}
		}
		else {
			$aTeam->create();
		}
		
		Login::lastInsert($aTeam->tag());
		
		// On purge le tableau de données
		$_POST["action"] = "";
		$_POST["teamName"] = "";
		$_POST["teamAdmin"] = "";
		$_POST["coachId"] = "";
		$_POST["d1Id"] = "";
		$_POST["d2Id"] = "";
		
		$text = $aTeam->displayLong();
		$head = $this->displayForm();
		$page = <<<EOS
<h2> Ajout effectué avec succès ! </h2>
${text}
EOS;
		return $head . $page;
	}

	private function displayForm($errors = array()) {
		$season = Season::read(Login::seasonChoosen(False));
		$season = $season->displayShort();
		
		$teamName  = PropRetriever::getProp($_POST,"teamName");
		$teamCat   = PropRetriever::getProp($_POST,"teamCat");
		$teamAdmin = PropRetriever::getProp($_POST,"teamAdmin");
		$coachId   = PropRetriever::getProp($_POST,"coachId");
		$d1Id      = PropRetriever::getProp($_POST,"d1Id");
		$d2Id      = PropRetriever::getProp($_POST,"d2Id");
		
		$DIRECTOR = 5;
		$COACH = 3;
		$UNKNOWN = "<option value=\"-1\"> Inconnu </option>";
		
		$functionsArray = array($COACH, $DIRECTOR);
		$members = Member::getOnFunction($functionsArray,
																		 Login::seasonChoosen(False));
		
		$coachList = Member::getAsList($members[$COACH],$coachId);
		$d1List    = $UNKNOWN . Member::getAsList($members[$DIRECTOR],$d1Id);
		$d2List    = $UNKNOWN . Member::getAsList($members[$DIRECTOR],$d2Id);
		
		$catOptions = "";
		$adminOptions = User::getAsList(1,$teamAdmin);
		$catOptions = TeamCategory::getAsList(Login::seasonChoosen(False),$teamCat);
		
		$style = array();
		$style["teamName"] = ""; $style["teamCat"] = "";
		
		if (($tmp = PropRetriever::getProp($errors,"teamName")) != "")
			$style["teamName"] = "class=\"error\"";
			
		if (($tmp = PropRetriever::getProp($errors,"teamCat")) != "")
			$style["teamCat"] = "class=\"error\"";
		
		$errorsText = "";
		foreach ($errors as $k => $v) {
			$errorsText .= "<li> <span class=\"error\"> $v </span> </li>";
		}
		if ($errorsText != "") {
			$tmp = "<h2> Des erreurs sont survenues ... </h2>";
			$tmp .= "<ul> " . $errorsText . " </ul>";
			$errorsText = $tmp;
		}
		
		$page = <<<EOS
<h1> Création d'une équipe pour la saison [ {$season} ] </h1>

<div align="center">
<br /> <br />
<form method="post" accept-charset="utf-8" name="addTeam"
			action="?add" >

<input type="hidden" name="action" value="add" />

<table> 

<tr> 
<td align="center">

	<fieldset>
		<legend> Informations Administratives </legend>
		<br />
		
		
		<label> 
			<span ${style["teamName"]}>Nom : </span>
			<input type="text" name="teamName" class="edit" />
			&nbsp; &nbsp;
		</label>
		<br />		
		
		<label> 
			<span ${style["teamCat"]}>Categorie : </span>
			<select name="teamCat" class="edit" />
			${catOptions}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span> Administrateur : </span>
			<select name="teamAdmin" class="edit" />
			${adminOptions}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		

		<br />
	</fieldset>
	
</td>

<td> &nbsp;&nbsp;&nbsp;&nbsp; </td>

<td align="center">
  <fieldset>
		<legend> Staff de l'équipe </legend>
		<br />
		
		<label> 
			<span>Entraineur : </span>
			<select name="coachId" class="edit" />
			${coachList}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span>Dirigeant [1] : </span>
			<select name="d1Id" class="edit" />
			${d1List}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span>Dirigeant [2] : </span>
			<select name="d2Id" class="edit" />
			${d2List}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />

		<br /> 
	</fieldset>
</td>
</tr>

<tr>
  <td align="center" colspan="3">
  	<br />
    <input type="submit" value="Ajouter l'équipe dans la base de données" class="edit" />
  </td>
</tr>
</table>
</div>
</form>

${errorsText}
EOS;
		return $page;
	}


}
?>